package i2.application.checklist.entities;


import static javax.persistence.GenerationType.AUTO;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "question")
@NamedQueries({
	@NamedQuery(name = "Question.findAllByCategorie", query = "SELECT q FROM Question q WHERE q.categorie.id = :categorieId"),
	@NamedQuery(name = "Question.hasReponses", query = "select 1 from Question q, Reponse r where q.id = :id and r.question.id = q.id")
})
public class Question implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue(strategy = AUTO)
	@Column(name = "id", unique = true, nullable = false)
	private int id;
		
	@ManyToOne(fetch = FetchType.LAZY) //TODO vérifier ça
	@JoinColumn(name = "categorie_id", nullable = false)
	private Categorie categorie;
	
	@Column(name = "question")
	private String question;
	
	@Column(name="description", nullable = true, length = 250 )
	private String description;
	
	@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinColumn(name = "alerte_id")
	private Alerte alerte;
	
	@Column(name = "yes")
	private boolean yesIsAGoodResponse = true;
	
	public Question() {
	}
	
	public Question(int id) {
		this.id = id;
	}
	
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public Categorie getCategorie() {
		return this.categorie;
	}

	public void setCategorie(Categorie categorie) {
		this.categorie = categorie;
	}
		
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getQuestion() {
		return question;
	}

	public void setQuestion(String question) {
		this.question = question;
	}

	public Alerte getAlerte() {
		return alerte;
	}

	public void setAlerte(Alerte alerte) {
		this.alerte = alerte;
	}
	
	public boolean isYesIsAGoodResponse() {
		return yesIsAGoodResponse;
	}

	public void setYesIsAGoodResponse(boolean yesIsAGoodResponse) {
		this.yesIsAGoodResponse = yesIsAGoodResponse;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Question other = (Question) obj;
		if (id != other.id) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "Question [id=" + id + ", categorie=" + categorie
				+ ", question=" + question + ", description=" + description
				+ "]";
	}
	
}
